const assert = require('assert')

const run = async (driver, vars, testCase, defaultBefore, defaultExecute, defaultAfter) => {
  // 手动测试结果
  const manuTestResult = testCase.extra?.manualTestResult;
  if (manuTestResult !== undefined) {
    assert(manuTestResult, testCase.extra?.manualTestDesc);
    testCase.status = manuTestResult ? 'passed' : 'failed';
    return;
  }

  await (async (...fns) => {
    for (const fn of fns) {
      if (fn instanceof Function) {
        await fn(driver, vars, testCase);
      }
    }
  })(
    // 执行前置操作，比如用户名生成
    testCase.before || defaultBefore
    // 执行测试用例，包括断言
    , testCase.execute || defaultExecute
    // 执行后置操作，也可以断言
    , testCase.after || defaultAfter
    , () => testCase.status = 'passed')

}

module.exports = {
  run
}
